﻿//************ app manage **************/

$.extend($.WishWall2, {
    mask: null,
	pageSize: 50,          // 每页最大纸条数目
	
    // resourceLoad: null,
    requestCount: 1,       // 当前加载请求数     //body.html请求时没有增加此变量，但请求结束后却减了此变量
    
	requestingFlag:false,  // 非并发请求标志  
	
    voteValidateType: new Array('image','text'),
    showLoading: function() {
        $.WishWall2.requestCount++;

		//alert($.WishWall2.requestCount);
		$.WishWall2.resourceLoad.show();
    },
    hideLoading: function() {
        if ($.WishWall2.resourceLoad) {
            $.WishWall2.requestCount--;
			
			//alert($.WishWall2.requestCount);
            if ($.WishWall2.requestCount <= 0) {
				
                $.WishWall2.resourceLoad.hide();
            }
        }
    },
	validateRequest: function(){
		if($.WishWall2.requestingFlag){
			$.WishWall2.showMsg('另一个请求正在处理中，请稍后重试');
			return false;
		}
		else{
			$.WishWall2.requestingFlag = true;
			//alert($.WishWall2.requestingFlag);
			//$.WishWall2.showLoading();
			return true;	
		}
	},
	requestFinished:function(){
		$.WishWall2.requestingFlag = false;
		//alert($.WishWall2.requestingFlag);
		//$.WishWall2.hideLoading();
	},
	showMsg:function(msg){
		alert('showMsg:' + msg);
	},
    showMask: function() {
        if (!$.WishWall2.mask) {
            $.WishWall2.mask = $('<div id="app-mask"></div>').addClass('app-mask').appendTo($(document.body));

            // 浏览器高度和宽度
            var sWidth = Math.max(document.body.scrollWidth, document.documentElement.clientWidth);
            var sHeight = Math.max(document.body.scrollHeight, document.documentElement.clientHeight);
            $.WishWall2.mask.css({ 'width': sWidth, 'height': sHeight }).dblclick(function(){$(this).hide();$('#operations').hide()});
        };

        $.WishWall2.mask.show();
    },
    hideMask: function() {
        if ($.WishWall2.mask) {
            $.WishWall2.mask.hide();
        }
    },
	loadError:function(event, XMLHttpRequest, ajaxOptions, thrownError){
		$.WishWall2.showMsg('错误');	
	}

});

/**************************/
$.extend($.WishWall2, {
    loadApp: function() {
        // 主加载提示手动隐藏  --原因：在show.aspx里设置了变量autoHide=false
		$('#wish-wall-loading').hide();
		
		// 全局加载等待提示
		$.WishWall2.resourceLoad = $('<div id="app-resource-loading">正在加载...</div>')
									.addClass('app-resource-loading')
									.appendTo($(document.body))
									.bind('ajaxSend', $.WishWall2.showLoading)
						  			.bind('ajaxComplete', $.WishWall2.hideLoading)
									.bind('ajaxError', $.WishWall2.loadError);
		
        //$.WishWall2.loadWishes(Subject.id, 1);

		$('div.txt_subject').text(Subject.title);
		$('div.txt_announce').text(Subject.announce);
		
        // 支持拖拽
        
        $('#announce').draggable();
		$('#pager').draggable();

		// 加载纸条，并产生分页控件
		$.WishWall2.loadWishes(Subject.id,1);


        //**************播放背景音乐********************
        var musicName = null;
        musicName = '520.mp3';
        if (musicName != null) {
            $("#music").jPlayer({
                swfPath: jsRoot,
                ready: function() {
                    //$('#music').setFile('../music/520.mp3').play(); // Defines the mp3 And auto play
                    $('#music').changeAndPlay(musicRoot + musicName);
                }
            });
            // 循环播放
            $('#music').onSoundComplete(function() {
                $('#music').changeAndPlay(musicRoot + musicName);
            });
        }
        
        //**************事件绑定********************
        $.WishWall2.AddButtonClick();
        $.WishWall2.ListButtonClick();
    },
    // 加载纸条
    loadWishes: function(sid, page) {

		if(!$.WishWall2.validateRequest())
			return false;

        $.ajax({
            type: "get",
            //contentType: "application/json",
            dataType: "json",
            url: ajaxRoot + "WishHandler.ashx",
            data: { "op": "gets", "sid": sid, "page":page },
            timeout: 10000,
			cache:false,
			//global:false,
			complete:function(){
				$.WishWall2.requestFinished();
			},
            success: function(msg) {
				//---------- 生成纸条---------------
                $('div.wish').remove();
                var wishes = msg.data.wishes;
				var count = msg.data.count;
				wishes.reverse();
				
                $.each(wishes, function(i, w) {
                    $.fn.jWish({
                        z: i,
                        id: i + 1,
                        title: '字条编号：' + w.wid,     // 标题
                        content: w.words,                // 内容
                        from: w.sender,                  // 发送人
                        date: w.createTime,              // 日期
                        icon: w.img,                     // 纸条图片
                        area: { left: Subject.theme.areaLeft, top: Subject.theme.areaTop, right: Subject.theme.areaRight, bottom: Subject.theme.areaBottom },
                        skin: getSkin(w.shape, w.color)
                    });
                });

                $('div.wish').draggable();
				
				//*------------- 生成分页控件 -----------------
				function PageClick(pageclickednumber) {
					$.WishWall2.loadWishes(sid, pageclickednumber);
					//$("#result").html("Clicked Page " + pageclickednumber);
				}
				
				var pageSize = $.WishWall2.pageSize;
				var pageCount = parseInt((count + pageSize - 1)/pageSize);
				$("#pager").pager({ pagenumber: page, pagecount: pageCount, buttonClickCallback: PageClick });
				
            }
        });
    }
});